#include <iostream>
#include <string>

using namespace std;

int main()
{
    int t;
    cin >> t;
    int score, tmp;
    while (t--)
    {
        int n, k;
        cin >> n >> k;
        score = tmp = 0;
        for (int i = 0; i < n; i++)
        {
            char ch;
            cin >> ch;
            if (ch == 'W')
            {
                tmp++;
                if (tmp >= 3)score += k;
                else score++;
            }
            else tmp = 0, score--;
        }
        cout << score << endl;
    }
    return 0;
}